package com.hsmpay.mobile.action.account.qian48;

import com.hsmpay.common.action.BaseAction;
import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.NumberUtil;
import com.hsmpay.common.util.URLDecoderTools;
import com.hsmpay.mobile.pojo.trans.TransOrder;
import com.hsmpay.mobile.pojo.trans.TransPriorSettle;
import com.hsmpay.mobile.service.trans.TransOrderService;
import com.hsmpay.mobile.service.trans.TransPriorSettleService;
import com.hsmpay.mobile.service.user.UserService;
import com.hsmpay.utils.Des3;
import it.sauronsoftware.base64.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 交易凭条 action
 *
 * @author 颜铃璋
 * @date 2014-12-10
 */
@Controller("q4CertificateAction")
@Scope("prototype")
public class Q4CertificateAction extends BaseAction {
    private static final long serialVersionUID = 5458501960593436913L;
    private static Logger log = Logger.getLogger(Q4CertificateAction.class);

    private Long psId;//收款Id
    private Long type;//终端类型
    private String d;//传输数据
    private TransPriorSettle transPriorSettle;
    private TransOrder transOrder;
    @Resource(name = "transPriorSettleService")
    private TransPriorSettleService<TransPriorSettle, Long> transPriorSettleService;
    @Resource(name = "transOrderService")
    protected TransOrderService<TransOrder, Long> transOrderService;
    @Resource(name = "userService")
    protected UserService<User, Long> userService;

    public Map<String, Object> certificateMap;

    /**
     * 凭证 http://localhost:8080/poc/c.action?d=NF80OTQxNg%3D%3D
     *
     * @return
     * @throws Exception
     */
    public String searchCertificate() throws Exception {
        if (StringUtils.isNotBlank(d)) {//使用加密
            d = URLDecoderTools.decodeUTF8(d);
            String dStr = new String(Des3.decodeXP(d));
            String[] dStrArray = dStr.split("_");
            type = Long.parseLong(dStrArray[0]);
            psId = Long.parseLong(dStrArray[1]);
        } else {
            this.sendAjaxResponse("非法操作，凭证已过期！");
            return null;
        }

        if (null == psId || null == type) {
            log.warn("凭证, 传入参数为空");
            return null;
        }

        

        try {
            transPriorSettle = new TransPriorSettle();
            transPriorSettle.setId(psId);
            transPriorSettle.setStatus(1);
            transPriorSettle = transPriorSettleService.searchEntity(transPriorSettle);

            if (null != transPriorSettle) {
                certificateMap = new HashMap<String, Object>();

                transOrder = new TransOrder();
                transOrder.setOrderNum(transPriorSettle.getOrderNum());
                transOrder = transOrderService.searchEntity(transOrder);
                if (null != transOrder) {
                    User user = new User();
                    user.setId(transOrder.getUserId());
                    user = userService.searchEntity(user);
                    transOrder.setUserName(user.getUserName());

                    String acquirersName = user.getOpeningBank();//修改收单行
                    if (null != acquirersName) {
                        if (!acquirersName.equals("中国银行"))
                            acquirersName = acquirersName.replace("中国", "");
                        transPriorSettle.setAcquirersName(acquirersName);
                    }
                    String authCode = transPriorSettle.getAuthorizationCode();
                    String searchId = transPriorSettle.getSearchId();
                    if (15 == transPriorSettle.getCollectionType()) {
                        searchId = authCode;
                        authCode = "000000";
                    }

                    certificateMap.put("userName", user.getUserName());//商户名称(MERCHANT NAME)
                    certificateMap.put("merchantId", transPriorSettle.getMerchantId());//商户号(MERCHANT NO)
                    certificateMap.put("terminalId", transPriorSettle.getTerminalId());//终端号(TERMINAL NO)  user.getId()
                    certificateMap.put("bankCard", NumberUtil.cardXing(transOrder.getBankCard()));//卡号(CARD NO)
                    certificateMap.put("acquirersName", acquirersName);//收单行名(ACQUIRER)
                    certificateMap.put("issueBank", transPriorSettle.getIssueBank());//发卡行名(ISSUER)
                    certificateMap.put("batchId", transPriorSettle.getBatchId());//批次号(BATCH NO)
                    certificateMap.put("searchId", "".equals(searchId) ? "000000" : searchId);//凭证号(VOUCHER NO)
                    certificateMap.put("authorizationCode", "".equals(authCode) ? "000000" : authCode);//授权码(AUTH NO)
                    certificateMap.put("bankRef", transPriorSettle.getBankRef());//参考号(REFER NO)

                    certificateMap.put("transDate", DateConvertUtil.dateString(transOrder.getCreateDate(), "yyyy/MM/dd"));//交易日期(DATE)
                    certificateMap.put("transTime", DateConvertUtil.dateString(transOrder.getCreateDate(), "HH:mm:ss"));//交易日期(DATE)
                    certificateMap.put("transMoney", transOrder.getTransMoney());//金额(AMOUNT)
                    certificateMap.put("signaturePath", transOrder.getSignaturePath());//持卡人签名(CARDHOLDER SINGATURE)

                    return "certResult";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return null;
    }

    public static void main(String args[]) throws Exception {
        String s = "MTRfNzExMDQy";
        String d = URLDecoderTools.decodeUTF8(s);
        String dStr = new String(Des3.decode(d));
        System.out.println(dStr);

        String a = "807696";
        System.out.println(a.substring(a.length() - 3, a.length()));
    }

    public Long getPsId() {
        return psId;
    }

    public void setPsId(Long psId) {
        this.psId = psId;
    }

    public Long getType() {
        return type;
    }

    public void setType(Long type) {
        this.type = type;
    }

    public String getD() {
        return d;
    }

    public void setD(String d) {
        this.d = d;
    }

    public Map<String, Object> getCertificateMap() {
        return certificateMap;
    }
}